//
//  Comms_I2C.c
//  ARDUINO_PROPELLER
//
//  Created by FRANCIS PAPINEAU on 12-02-06.
//  Copyright 2012 carleton university. All rights reserved.
//

// Headers:
    // - Errors - '%'
    // - Data - '@'
    // - Hardware INT - '$'
    // - Sys. Info - '*' -> State
    // - Toggle DEBUG - '-'
    
    // Tail - '+'

// --------------------------------------------------------------

void wait_For_Request(void){
    
    char request;
    
    for(;;){
        if(Serial2.available() > 0){
            request = Serial.read();
            if(request == '!'){// data request
                break;
            }else if(request == 'S'){ // sleep
                Sleep();
                break;
            }else if(request == 's'){ // state
                sendState();
            }else if(request == 'D'){ // debug
                toggleDebug();
                break;
            }
            else if(request == 'p'){ // start / stop
                start_Stop();
                break;
            }
            else if (request == 'e'){
                Serial2.print('-');
                Serial1.print('-');
                Serial2.print(request);
                Serial1.print(request);
            }
        }
    }
}

// --------------------------------------------------------------

void send_Data(string buffer){
    
    // The header for the receiving end of the Serial Network
    Serial1.print('@');
    
    Serial1.print(buffer);
    
    // The Checksum Character.
    Serial1.print('+');
    
    Serial2.print('@');
    
    Serial2.print(buffer);
    
    // The Checksum Character.
    Serial2.print('+');
}

// --------------------------------------------------------------

void send_INT_Header_DATA(void){
    
    // The header for the receiving end of the Serial Network
    Serial1.print('$');
    
    Serial1.print(pinInterrupt);
    
    // The Checksum Character.
    Serial1.print('+');
    
    Serial2.print('$');
    
    Serial2.print(pinInterrupt);
    
    // The Checksum Character.
    Serial2.print('+');
    
    pinInterrupt = 0;
    
}

// --------------------------------------------------------------

void send_INT_Header_DEBUG(void){
    
    // The header for the receiving end of the Serial Network
    Serial1.print('$');
    
    Serial1.print(pinInterrupt);
    
    // The Checksum Character.
    Serial1.print('+');
    
    Serial2.print('$');
    
    Serial2.print(pinInterrupt);
    
    // The Checksum Character.
    Serial2.print('+');
    
    pinInterrupt = 0;
    
}

// --------------------------------------------------------------

void sendState(void){
    
    // The header for the receiving end of the Serial Network
    Serial1.print('*');
    
    Serial1.print(state);
    
    // The Checksum Character.
    Serial1.print('+');
    
    Serial2.print('*');
    
    Serial2.print(state);
    
    // The Checksum Character.
    Serial2.print('+');
}

// --------------------------------------------------------------

void sendError(bool Error, int Address){
    
    //! This function is used to send Debug information to the Computer on the GS
    //! it sends the Error type and the Address of that error based on the Error
    //! table.
    
    //! it has a redundancy check using the Error variable, and also has a array
    //! location where the address can be found. Using this we can debug our system
    //! a lot better.
    
    //! check for !Error and then return the ErrorTableAddress[Address] and 
    //!                                      ErrorTableString[Address]
    
    ERRORS++;
    
    changeStateTo(4);
    
    if(!Error){
        Serial1.print('%');
        Serial1.print(ErrorTableAddress[Address]);
        Serial1.print("           ");
        Serial1.print(ErrorTableString[Address]);
        Serial1.print('+');
        
        Serial2.print('%');
        Serial2.print(ErrorTableAddress[Address]);
        Serial2.print("           ");
        Serial2.print(ErrorTableString[Address]);
        Serial2.print('+');

    }
    toggleERRORLED();
    
}
